package net.asciiworks.filesyncr.view.contentPane {
    import net.asciiworks.filesyncr.ApplicationFacade;
    import net.asciiworks.filesyncr.model.DataProxy;
    
    import flash.events.Event;
    
    import org.puremvc.as3.interfaces.IMediator;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class SettingsMediator extends Mediator implements IMediator {
        public static const NAME:String = "SettingsMediator";
		
		private var dataProxy:DataProxy;

        public function SettingsMediator(viewComponent:Settings) {
            super(NAME, viewComponent);
        }

        override public function onRegister():void {
			dataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
			
			settings.addEventListener("save", saveHandler);

			settings.source.text = dataProxy.source;
			settings.destinationVolume.text = dataProxy.destinationVolume;
			settings.destination.text = dataProxy.destination;
        }

		private function saveHandler(event:Event):void {
			settings.canResave = false;
		}
		
        override public function listNotificationInterests():Array {
            return [ ApplicationFacade.SETTINGS_CHANGED];
        }

        override public function handleNotification(note:INotification):void {
            switch (note.getName()) {
                case ApplicationFacade.SETTINGS_CHANGED:
					settings.source.text = dataProxy.source;
					settings.destinationVolume.text = dataProxy.destinationVolume;
					settings.destination.text = dataProxy.destination;
					settings.canResave = true;
                    break;
            }
        }

        private function get settings():Settings {
            return viewComponent as Settings;
        }
    }
}